﻿
using UnityEngine;
using Framework.Mathd;
using System.Collections.Generic;
using Framework.Entity;
/**
 * Copyright 2018 北京伯睿科技有限公司.
 * All Rights Reserved.
 *  
 *
 * @ShipProperty
 * @ShipProperty 舟船属性类
 *
 * @Author 牛瑞杰
 * @Date 2019年9月5日
 * @Version 1.0
 */

namespace BridgeShips 
    {
   
    public class ShipProperty 
        {
        public enum ShipsPartType {
            flap,//挡板
            shortBridge,//短桥节
            middleBridge,//中桥节
            openUpBridge,//展直桥节
            None
        }
        public enum ShipsType {
            ship_Four ,
            ship_Five ,
            ship_Eight ,
            ship_Nine,
            ship_Ten ,
            line_ship_Four,
            line_ship_Five,
            line_ship_Eight,
            line_ship_Nine,
            line_ship_Ten,
            None
        }
        private uint id;
        private uint id1;
        private uint id2;
        private ShipsType type;
        private Vector3d firstPosition;
        private Vector3d secondPosition;
        private Vector3d centerPosition;
        private Quaternion currentHeading;
        private Quaternion lerpCurrentHeading;
        private Vector3 preHeading = Vector3.zero;
        private int flagBegin = 0;
        private float length;
        private float timer;

        private bool isValid = true;
        public float locateDir = -65.0f;
        private Vector3d m_oldLastCenterPosition = Vector3.zero;
        private Vector3 m_OldDirectionNor = Vector3d.zero;
        private LinkedList<Vector3> m_ListHeading = new LinkedList<Vector3>();   //平均每个船方向

        private void AvaerageHeading()
        {
            LinkedListNode<Vector3> currentNode = m_ListHeading.First;
           // preHeading = Vector3.zero;
            while (currentNode != null)
            {
                preHeading = Vector3.Lerp(currentNode.Value,currentNode.Value,0.5f);
                currentNode = currentNode.Next;
            }
            preHeading.y = 0.0f;
           // preHeading = preHeading / m_ListHeading.Count;
        }

        public void AddFirst(Vector3 value)
        {
            timer += Time.deltaTime;
            if (m_OldDirectionNor.Equals(Vector3.zero))
            {
                m_OldDirectionNor = value;
                preHeading = value;
            }

            float value1 =  Vector3.Angle(value, m_OldDirectionNor);
            if (Vector3.Angle(value, m_OldDirectionNor) < 10.0f)
            {
                if (timer >= 0.5f)
                {
                    m_ListHeading.AddFirst(value);
                    m_OldDirectionNor = value;
                    //CreateObj(value);
                    while (m_ListHeading.Count >= 5)
                    {
                        m_ListHeading.RemoveLast();
                    }
                  
                    if (m_ListHeading.Count >= 3)
                    {
                        AvaerageHeading();
                        if (preHeading.Equals(Vector3.zero))
                        {
                            preHeading = value;
                        }
                    }
                    timer = 0.0f;
                }
            }
            else
            {
                m_OldDirectionNor = Vector3.Lerp(m_OldDirectionNor, value,Mathf.Abs( Mathf.Sin(value1)));
            }
        }
        public bool Valid {
            get {
                if (true) {

                }
                return isValid;
            }
            set {
                isValid = value;
            }
        }

        public float LocateDir
        {
            get
            {
                return locateDir;
            }
            set
            {
                locateDir = value;
            }
        }

        public uint Id {
            get {
                if (true) {

                }
                return id;
            }
            set {
                id = value;
            }
        }
  
        public ShipsType Type {
            get {
                return type;
            }
            set {
                type = value;
            }
        }

        public Vector3d FirstPosition {
            get {
                return firstPosition;
            }
            set {
                firstPosition = value;
            }
        }

        public Vector3d SecondPosition {
            get {
                return secondPosition;
            }
            set {
                secondPosition = value;
            }
        }

        public uint Id1 {
            get {
                return id1;
            }
            set {
                id1 = value;
            }
        }

        public uint Id2 {
            get {
                return id2;
            }
            set {
                id2 = value;
            }
        }



        public Quaternion CurrentHeading {
            get {
                //if (m_oldLastCenterPosition==Vector3d.zero)
                //{
                //    m_oldLastCenterPosition = FirstPosition;
                //}
                //  Vector3d heading = (CenterPosition  - m_oldLastCenterPosition).normalized;//舟船前进的方向
                if (FirstPosition.Equals(SecondPosition) )
                    return Quaternion.identity;
               Vector3d locationDirection = SecondPosition - FirstPosition;
                locationDirection.y = 0.0f;
               double locationDis = locationDirection.magnitude;
                IEntity entity = GameEntry.Instance.EntityManager.Get(Id);
                if (locationDis > entity.GetAI().Settings.BridgeShipLenght)
                    return entity.Get().transform.localRotation;
                Vector3 directionNor = locationDirection.normalized; //两个坐标点之间的向量

               //  float angle = Vector3d.Angle(direction, heading);//舟船正前方和从1点到2点向量之间的夹角
               // Debug.LogError(angle);
                AddFirst(directionNor);
                
                Debug.DrawLine(FirstPosition, SecondPosition, Color.green, 10.0f);
              
                float adjustAngle = 0.0f;
                if(entity != null)
                {
                    ShipTypes sType = entity.GetAI().Settings.GetShipPattern();
                    if (sType == ShipTypes.StraddleType)
                    {
                       // adjustAngle = 100.0f;
                        locateDir =  GameController.DataAdjustControl.PrefabShipLocalAngle;
                    }
                    else if (sType == ShipTypes.LineType)
                    {
                       // adjustAngle = 11.0f;
                        locateDir =  GameController.DataAdjustControl.LineShipLocalAngle;
                    }
                }
                //ShipTypes sType = (int)ShipsDictionary.Instance.shipPrefabricateType[Id];
             
                Quaternion planerot = Quaternion.LookRotation(preHeading, Vector3.up);
                lerpCurrentHeading = Quaternion.Lerp(lerpCurrentHeading, planerot, Time.deltaTime);
                // locateDir = adjustAngle + GameController.DataAdjustControl.ShipLocalAngle ;
                currentHeading = lerpCurrentHeading * Quaternion.AngleAxis(locateDir, Vector3.up) ;
                return currentHeading;// Quaternion.LookRotation(heading); 
            }
            set {
                currentHeading = value;
            }
        }


        public Vector3d CenterPosition {
            get {
                centerPosition = (FirstPosition + SecondPosition) / 2;
               
                return new Vector3d( centerPosition.x,7.0f, centerPosition.z);
            }
            set {
                centerPosition = value;
            }
        }

        public float Length {
            get {
                if (type==ShipsType.ship_Four) {
                    length = 29.6f;
                }
                else if (type==ShipsType.ship_Five) {
                    length = 37f;
                }
                else if (type == ShipsType.ship_Nine) {
                    length = 67f;
                }
                else if (type == ShipsType.ship_Ten) {
                    length = 74f;
                }
                else if (type == ShipsType.None) {
                    length = 0f;
                }
                return length;
            }

        }
    }
}